package control 
{
	import events.MyEvent;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import loader.SingelPicLoadedHaveContainer;
	import utils.MyTimer;
	/**
	 * ...
	 * @author FireKnight
	 */
	public class ScrollManyPics 
	{
		private var pics_url:Vector.<String>;
		private var container:Sprite;
		private var _startX:Number;
		private var _startY:Number;
		private var _space:Number;
		private var loaders:Vector.<Sprite>;
		private var direction:String; // h代表横向 v代表纵向
		private var left:Number;
		private var right:Number;
		private var top:Number;
		private var bottom:Number;
		private var offset:Number = 500;
		private var first:Boolean = true;
		private var mask_mc:Sprite;
		public var fx:int = 2;
		public var distance:Number = 0;
		protected var startMouse:Number = 0;
		protected var endMouse:Number = 0;
		private var myTimer:MyTimer;
		private var interval:Number=0.01;
		public function ScrollManyPics(s:Vector.<String>,_container:Sprite,_mask:Sprite,displayRange:Rectangle,startX:Number=0,startY:Number=0,space:Number=0,_direction:String="h") 
		{
			pics_url = s;
			container = _container;
			_startX = startX;
			_startY = startY;
			_space = space;
			left = displayRange.left;
			right = displayRange.right;
			top = displayRange.top;
			bottom = displayRange.bottom;
			direction = _direction;
			mask_mc = _mask;
			mask_mc.addEventListener(MouseEvent.MOUSE_DOWN, handleMaskMouseDown);
			mask_mc.addEventListener(Event.ENTER_FRAME, handleMaskMcEnterFrame);
			work();
		}
		
		private function work():void
		{
			if (!loaders)
			{
				loaders = new Vector.<Sprite>();
				addSprite(0, 1);
			}
		}
		
		/**
		 * 
		 * @param	s 添加第s个url图片
		 * @param	b 添加的方向 1是后面 -1是前面
		 */
		private function addSprite(s:Number,b:Number):void
		{
			var curNum:Number = getCurNum(s);
			if (container.getChildByName("loader_" + curNum)) return;
			var _loader:Sprite = new Sprite();
			_loader.name = "loader_" + curNum;
			container.addChild(_loader);
			var single_pic_loader:SingelPicLoadedHaveContainer = new SingelPicLoadedHaveContainer(pics_url[curNum], positionAddedPicLoader,_loader,b);
		}
		
		
		private function getCurNum(s:Number):Number
		{
			if (s < 0)
			{
				s = pics_url.length + s;
			}
			else if (s >= pics_url.length)
			{
				s = s-pics_url.length;
			}
			return s;
		}
		
		private function positionAddedPicLoader(s:Sprite,addDirection:Number=1):void
		{
			if (loaders.length == 0)
			{
				s.y = _startY;
				s.x = _startX;
				loaders.push(s);
			}
			else
			{
				var num:Number;
				if (direction == "h")
				{
					s.y = _startY;
					if (addDirection == 1)
					{
						num = loaders.length - 1;
						s.x = loaders[num].x + loaders[num].width+_space;
						loaders.push(s);
					}
					else
					{
						
						s.x = loaders[0].x - s.width-_space;
						loaders.unshift(s);
					}
				}
				else
				{
					s.x = _startX;
					if (addDirection == 1)
					{
						
						num = loaders.length - 1;
						s.y = loaders[num].y + loaders[num].height+_space;
						loaders.push(s);
					}
					else
					{
						
						s.y = loaders[0].y - s.height-_space;
						loaders.unshift(s);
					}
				}
			}
			
		}
		
		private function getCurSprite(s:Sprite):Number
		{
			var sp:Number;
			for (var i:uint = 0; i < loaders.length; i++)
			{
				if (s == loaders[i])
				{
					sp = i;
					break;
				}
			}
			return sp;
		}
		
		private function positionPicLoader(s:Sprite,b:String,c:Number):void
		{
			if (b == "l")
			{
				s.x = c;
				if (s.x + s.width < left - offset)
				{
					removeSprite(s);
				}
			}
			else if (b == "r")
			{
				s.x = c;
				if (s.x > right + offset)
				{
					removeSprite(s);
				}
			}
			else if (b == "t")
			{
				s.y = c;
				if (s.y < top - offset)
				{
					removeSprite(s);
				}
			}
			else if (b== "b")
			{
				s.y = c;
				if (s.y > bottom + offset)
				{
					removeSprite(s);
				}
			}
			
			addNeedSprite();
			
		}
		
		private function correctionPosition():void
		{
			if (loaders.length > 2)
			{
				var middleNum:uint = Math.floor(loaders.length / 2);
				//trace("middleNum="+middleNum);
				for (var i:int = middleNum-1; i>-1; i--)
				{
					//trace("i=" + i);
					//trace("loaders[i]=" + loaders[i].name);
					//trace("loaders[i].width=" + loaders[i].width);
					
					if (direction == "h")
					{
						loaders[i].x = loaders[i + 1].x - loaders[i].width-_space;
					}
					else
					{
						loaders[i].y = loaders[i + 1].y - loaders[i].height-_space;
					}
				}
				for (var o:uint = middleNum + 1; o < loaders.length; o++)
				{
					//trace("o=" + o);
					//trace("loaders[o - 1]=" + loaders[o - 1].name);
					//trace("loaders[o - 1].width=" + loaders[o - 1].width);
					if (direction == "h")
					{
						loaders[o].x = loaders[o - 1].x + loaders[o - 1].width + _space;
					}
					else
					{
						loaders[o].y = loaders[o - 1].y + loaders[o - 1].height + _space;
					}
					
				}
				
			}
		}
		
		private function addNeedSprite():void
		{
			var num:Number;
			//trace("loaders.length="+loaders.length);
			if (loaders.length == 1)
			{
				num = Number(loaders[0].name.slice(7));
				addSprite(num - 1, -1);
				addSprite(num + 1, 1);
			}
			else if(loaders.length>1)
			{
				var first_item:Sprite=loaders[0];
				
				if (direction == "h")
				{
					if (first_item.x  > left-offset)
					{
						num = Number(first_item.name.slice(7));
						addSprite(num - 1, -1);
					}
					if (loaders.length > 1)
					{
						var last_item:Sprite = loaders[loaders.length - 1];
						if (last_item.x< right + offset)
						{
							num = Number(last_item.name.slice(7));
							addSprite(num + 1, 1);
						}
					}
					
				}
				else
				{
					if (first_item.y  > top-offset)
					{
						num = Number(first_item.name.slice(7));
						addSprite(num - 1, -1);
					}
					if (loaders.length > 1)
					{
						var _last_item:Sprite = loaders[loaders.length - 1];
						if (_last_item.y < bottom + offset)
						{
							num = Number(_last_item.name.slice(7));
							addSprite(num + 1, 1);
						}
					}
				}
			}
		}
		
		private function removeSprite(s:Sprite):void
		{
			var num:Number = getCurSprite(s);
			loaders.splice(num, 1);//删除数组中的元素
			container.removeChild(s);
			var bm:Bitmap = s.getChildAt(0) as Bitmap;
			if (bm)
			{
				bm.bitmapData.dispose();
			}
			s = null;
		}
		
		private function handleMaskMouseDown(e:MouseEvent):void
		{
			distance = 0;
			if (direction == "h")
			{
				startMouse = mask_mc.parent.mouseX;
			}
			else
			{
				startMouse = mask_mc.parent.mouseY;
			}
			
			mask_mc.parent.addEventListener(MouseEvent.MOUSE_UP, handleMaskMouseUp);
			mask_mc.parent.addEventListener(MouseEvent.MOUSE_MOVE, handleMaskMouseMove);
			mask_mc.parent.dispatchEvent(new MyEvent("moved", null, true));
			removeMyTimer();
		}
		
		private function handleMaskMouseMove(e:MouseEvent):void
		{
			
			if (direction == "h")
			{
				endMouse = mask_mc.parent.mouseX;
			}
			else
			{
				endMouse = mask_mc.parent.mouseY;
			}
			distance = (startMouse-endMouse);
			moveFunction();
			mask_mc.parent.dispatchEvent(new MyEvent("moved", null, true));
			if (direction == "h")
			{
				startMouse = mask_mc.parent.mouseX;
			}
			else
			{
				startMouse = mask_mc.parent.mouseY;
			}
		}
		
		protected function moveFunction():void
		{
			var target:Number = 0 - distance;
			
			if (direction == "h")
			{
				if (target > 0)
				{
					fx = 2;
					updatePositions("r", target);
				}
				else if(target<0)
				{
					fx = -2;
					updatePositions("l", target);
				}
				else
				{
					updatePositions("c", target);
				}
			}
			else
			{
				if (target > 0)
				{
					fx = 2;
					updatePositions("t", target);
				}
				else if(target<0)
				{
					fx = -2;
					updatePositions("b", target);
				}
				else
				{
					updatePositions("c", target);
				}
			}
		}
		
		public function addMyTimer():void
		{
			removeMyTimer();
			myTimer = new MyTimer(handleTimerEvent, interval);
			myTimer.start();
		}
		
		private function removeMyTimer():void
		{
			if (myTimer)
			{
				myTimer.stop();
				myTimer.GC();
			}
		}
		
		
		
		private function handleTimerEvent():void
		{
			if (direction == "h")
			{
				if (fx == 2)
				{
					updatePositions("r", fx);
				}
				else if (fx == -2)
				{
					updatePositions("l", fx);	
				}
			}
			else
			{
				if (fx == 2)
				{
					updatePositions("t", fx);
				}
				else if (fx == -2)
				{
					updatePositions("b", fx);	
				}
			}
		}
		
		private function handleMaskMouseUp(e:MouseEvent):void
		{
			updatePositions("c", 0);
			addMyTimer();
			mask_mc.parent.removeEventListener(MouseEvent.MOUSE_UP, handleMaskMouseUp);
			mask_mc.parent.removeEventListener(MouseEvent.MOUSE_MOVE, handleMaskMouseMove);
		}
		
		private function handleMaskMcEnterFrame(e:Event):void
		{
			updatePositions("c", 0);
			correctionPosition();
		}
		
		/**
		 * 
		 * @param	s	方向 接受 l,r,t,b,c
		 * @param	b	偏移值 
		 */
		public function updatePositions(s:String,b:Number):void
		{
			if (loaders)
			{
				for (var i:uint = 0; i < loaders.length; i++)
				{
					var target:Number = loaders[i].x += b;
					positionPicLoader(loaders[i],s,target);
				}
			}
			
		}
		
		public function get pics():Vector.<Sprite>
		{
			return loaders;
		}
		
		private function removeLoaders():void
		{
			if (loaders)
			{
				for (var i:uint = 0; i < loaders.length; i++)
				{
					removeSprite(loaders[i]);
				}
				loaders = null;
			}
		}
		
		public function gc():void
		{
			removeMyTimer();
			removeLoaders();
			pics_url = null;
			mask_mc.removeEventListener(MouseEvent.MOUSE_DOWN, handleMaskMouseDown);
			mask_mc.removeEventListener(Event.ENTER_FRAME, handleMaskMcEnterFrame);
		}
		
	}

}